﻿using System;
using System.Collections.Specialized;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using Hvam.SharePoint.Search.Query;

namespace Hvam.SharePoint.Search.Tests.Public.Query.Site
{
    [TestClass]
    public class SiteSearchRuntimeTests
    {
        [TestMethod]
        public void WelcomePageSPSiteDataQueryTest()
        {
            // Arrange
            TestTools.EnsureLocation();

            using (var site = new SPSite(TestConstants.Site))
            {
                var serviceContext = SPServiceContext.GetContext(site);

                var searchProxy = (SearchServiceApplicationProxy) serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy));

                var properties = new StringCollection { "title", "path", "author", "sitetitle", "size", "description", "write" };

                const int startItem = 1;

                var queryManager = new QueryManager();
                var locationList =
                    new LocationList
                        {
                            new Location(TestConstants.SiteSearchLocationName, searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20, 
                                    TrimDuplicates = false
                                },
                            new Location("LocalSearchIndex", searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20,
                                    TrimDuplicates = false
                                }
                        };

                foreach (var location in locationList)
                {
                    var sortList = location.GetSortList();

                    sortList.Clear();
                    sortList.Add("Title", SortDirection.Ascending);
                }

                queryManager.UserQuery = "site:\"http://sharepoint/sites/sitesearch/\" contenttype:\"Welcome Page\"";
                queryManager.Add(locationList);
                queryManager.IsTriggered(locationList);

                // Act
                var xmlDocument = queryManager.GetResults(locationList);

                // Assert
                Assert.IsNotNull(xmlDocument, "XmlDocument is null.");
                Console.WriteLine(XElement.Parse(xmlDocument.OuterXml).ToString());
            }
        }

        [TestMethod]
        public void ReusableHtmlSPSiteDataQueryTest()
        {
            // Arrange
            TestTools.EnsureLocation();

            using (var site = new SPSite(TestConstants.Site))
            {
                var serviceContext = SPServiceContext.GetContext(site);

                var searchProxy = (SearchServiceApplicationProxy) serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy));

                var properties = new StringCollection { "title", "path", "author", "sitetitle", "write" };
                const int startItem = 1;

                var queryManager = new QueryManager();
                var locationList =
                    new LocationList
                        {
                            new Location(TestConstants.SiteSearchLocationName, searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20, 
                                    TrimDuplicates = false
                                },
                            new Location("LocalSearchIndex", searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20,
                                    TrimDuplicates = false
                                }
                        };

                foreach (var location in locationList)
                {
                    var sortList = location.GetSortList();

                    sortList.Clear();
                    sortList.Add("Title", SortDirection.Ascending);
                }

                queryManager.UserQuery = "site:\"http://sharepoint/sites/sitesearch/\" contenttype:\"Reusable HTML\"";
                queryManager.Add(locationList);
                queryManager.IsTriggered(locationList);

                // Act
                var xmlDocument = queryManager.GetResults(locationList);

                // Assert
                Assert.IsNotNull(xmlDocument, "XmlDocument is null.");
                Console.WriteLine(XElement.Parse(xmlDocument.OuterXml).ToString());
            }
        }

        [TestMethod]
        public void ReusableHtmlSPQueryTest()
        {
            // Arrange
            TestTools.EnsureLocation();

            using (var site = new SPSite(TestConstants.Site))
            {
                var serviceContext = SPServiceContext.GetContext(site);

                var searchProxy = (SearchServiceApplicationProxy) serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy));

                var properties = new StringCollection { "title", "path", "author", "sitetitle" };
                const int startItem = 1;

                var queryManager = new QueryManager();
                var locationList = 
                    new LocationList
                        {
                            new Location(TestConstants.SiteSearchLocationName, searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20, 
                                    TrimDuplicates = false
                                },
                            new Location("LocalSearchIndex", searchProxy)
                                {
                                    RequestedProperties = properties,
                                    StartItem = startItem,
                                    ItemsPerPage = 20,
                                    TrimDuplicates = false
                                }
                        };

                foreach (var location in locationList)
                {
                    var sortList = location.GetSortList();

                    sortList.Clear();
                    sortList.Add("Title", SortDirection.Ascending);
                }

                queryManager.UserQuery = "site:\"http://sharepoint/sites/sitesearch/ReusableContent/\" contenttype:\"Reusable HTML\"";
                queryManager.Add(locationList);
                queryManager.IsTriggered(locationList);

                // Act
                var xmlDocument = queryManager.GetResults(locationList);

                // Assert
                Assert.IsNotNull(xmlDocument, "XmlDocument is null.");
                Console.WriteLine(XElement.Parse(xmlDocument.OuterXml).ToString());
            }
        }
    }
}
